package test;

/**
 * 动态规划求k阶裴波那契序列
 */
public class Fibonacci {
    private int factor;     //裴波那契序列阶数
    private int length;     //数列长度
    private int [] fibo;    //数列数组
    /**
     * length:数组大小
     * factor:阶数（阶数大于1）
     */
    public Fibonacci(int length,int factor){
        if(factor<2){
            System.out.println("阶数必须大于1");
            factor=2;
        }
        this.factor=factor;
        this.length=length;
        this.fibo=new int[length];
        for(int i=0;i<factor-1;i++){
            this.fibo[i]=0;
        }
        this.fibo[factor-1]=1;
        this.print();
    }
    /**
     * pos:数列项数
     */
    public int dp(int pos){
        for(int i=this.factor;i<this.length;i++){
            for(int j=i-1;j>=i-this.factor;j--){
                this.fibo[i]+=this.fibo[j];
            }
            this.print();
            if(i==pos-1){
                return this.fibo[pos-1];
            }
        }
        return 0;
    }
    /**
     * 打印数组
     */
    public void print(){
        for(int i=0;i<this.fibo.length;i++){
            System.out.print(this.fibo[i]+"    ");
        }
        System.out.println();
    }
    public static void main(String [] args){
        Fibonacci fino=new Fibonacci(10,4);
        int value=fino.dp(15);
        System.out.println(value);
    }
}
